package stock.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;
import java.time.Duration;
/**
 * @author linkanyway
 * @version 1.0
 * @name LettuceRedisConfig
 * @description TODO
 * @date 2022/01/11 22:44
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Slf4j
public class LettuceConfig {
    private String host;
    private String password;
    private int port;
    private int database;
    private int maxIdle;
    private int minIdle;
    private int maxWaitMillis;
    private int maxTotal;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getDatabase() {
        return database;
    }

    public void setDatabase(int database) {
        this.database = database;
    }

    public int getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(int maxIdle) {
        this.maxIdle = maxIdle;
    }

    public int getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(int minIdle) {
        this.minIdle = minIdle;
    }

    public int getMaxWaitMillis() {
        return maxWaitMillis;
    }

    public void setMaxWaitMillis(int maxWaitMillis) {
        this.maxWaitMillis = maxWaitMillis;
    }

    public int getMaxTotal() {
        return maxTotal;
    }

    public void setMaxTotal(int maxTotal) {
        this.maxTotal = maxTotal;
    }

    /**
     * LettuceConnectionFactory
     *
     * @return
     */
//    @Bean
//    public LettuceConnectionFactory redisConnectionFactory() {
//        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
//        redisStandaloneConfiguration.setHostName(host);
//        redisStandaloneConfiguration.setPort(port);
//        redisStandaloneConfiguration.setPassword(password);
//        redisStandaloneConfiguration.setDatabase(database);
//        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig ();
//        poolConfig.setMaxIdle(maxIdle);
//        poolConfig.setMinIdle(minIdle);
//        poolConfig.setMaxWaitMillis(maxWaitMillis);
//        poolConfig.setMaxTotal(maxTotal);
//        LettucePoolingClientConfiguration lettucePoolingClientConfiguration =
//                LettucePoolingClientConfiguration.builder ().commandTimeout (Duration.ofSeconds (10)).shutdownTimeout (Duration.ZERO).poolConfig (poolConfig).build ();
//        LettuceConnectionFactory lettuceConnectionFactory =
//                new LettuceConnectionFactory (redisStandaloneConfiguration, lettucePoolingClientConfiguration);
//        lettuceConnectionFactory.setShareNativeConnection (false);
//        log.info("LettuceConnectionFactory Bean注册");
//
//        return lettuceConnectionFactory;
//    }
//    /**
//     * RedisTemplate
//     *
//     * @param connectionFactory
//     * @return
//     */
//    @Bean
//    public RedisTemplate redisLettuceTemplate(@Autowired LettuceConnectionFactory connectionFactory) {
//        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<> ();
//        redisTemplate.setKeySerializer (new StringRedisSerializer ());
//        redisTemplate.setValueSerializer (new GenericJackson2JsonRedisSerializer ());
//        redisTemplate.setConnectionFactory (connectionFactory);
//        log.error("redisLettuceTemplate 参数"+connectionFactory.getHostName()+connectionFactory.getPassword() + connectionFactory.getStandaloneConfiguration());
//        return redisTemplate;
//    }
//    /**
//     * @param factory
//     * @return
//     */
//    @Bean("stringRedisLettuceTemplate")
//    public StringRedisTemplate stringRedisLettuceTemplate(@Autowired LettuceConnectionFactory factory) {
//        StringRedisTemplate template = new StringRedisTemplate (factory);
//        template.setEnableTransactionSupport (true);
//        ObjectMapper mapper;
//        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer ();
//        template.setValueSerializer (new StringRedisSerializer ());
//        template.setKeySerializer (new StringRedisSerializer ());
//        template.setHashKeySerializer (new StringRedisSerializer ());
//        template.setHashValueSerializer (new StringRedisSerializer ());
//        template.afterPropertiesSet ();
//        return template;
//    }
}
